package system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.input.RichInputText;

import view.common.Utils;

public class SystemRole {

    public SystemRole() {
        super();
        if (Boolean.FALSE.equals(Utils.getElExpression("#{adfFacesContext.postback}"))) {
            listGroups();
        }
    }

    public void listGroups() {
        ManageGroup objWeblogicGroup = new ManageGroup();
        List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();

        String listGroup;
        try {
            listGroup = objWeblogicGroup.listGroups();
            EnumHeThong.enumPagePermission[] lstEnumGroup =
                EnumHeThong.enumPagePermission.values();
            List<String> listPagePer = new ArrayList();
            for (int i = 0; i < lstEnumGroup.length; i++) {
                listPagePer.add(lstEnumGroup[i].getPermissionCode());
            }
            while (objWeblogicGroup.haveCurrent(listGroup)) {
                String group = objWeblogicGroup.getCurrentName(listGroup);
                String description =
                    objWeblogicGroup.getGroupDescription(group);
                objWeblogicGroup.advance(listGroup);
                if (listPagePer.contains(group)) {
                    Map<String, Object> lstTemp =
                        new HashMap<String, Object>();
                    lstTemp.put("0", group);
                    lstTemp.put("1", description);
                    lst.add(lstTemp);
                }
            }
            ManageWeblogic.close();
            table.setValue(lst);
        } catch (Exception e) {
            Utils.showMessage(Utils.MSG_ERROR, e.getMessage());
        }
    }

    public void popGroup(ActionEvent actionEvent) {
        
        String paramUsername =
            actionEvent.getComponent().getAttributes().get("param").toString();
        Utils.showPopup(getPopup());
        txtTenGroup.setValue(paramUsername);
        try {
            txtDescription.setValue(new ManageGroup().getGroupDescription(paramUsername));
        } catch (Exception e) {
            Utils.showMessage(Utils.MSG_ERROR, e.getMessage());
        }
    }
    
    public void updateRoleDescription(ValueChangeEvent valueChangeEvent) {
        ManageGroup objWeblogicGroup = new ManageGroup();

        try {
            objWeblogicGroup.setGroupDescription((String)txtTenGroup.getValue(), (String)valueChangeEvent.getNewValue());
        } catch (Exception e) {
            Utils.showMessage(Utils.MSG_ERROR, e.getMessage());
        }
    }

    private RichTable table = new RichTable();
    private RichPopup popup = new RichPopup();
    private RichInputText txtTenGroup = new RichInputText();
    private RichInputText txtDescription = new RichInputText();

    public void setTable(RichTable table) {
        this.table = table;
    }

    public RichTable getTable() {
        return table;
    }

    public void setPopup(RichPopup popup) {
        this.popup = popup;
    }

    public RichPopup getPopup() {
        return popup;
    }


    public void setTxtTenGroup(RichInputText txtTenGroup) {
        this.txtTenGroup = txtTenGroup;
    }

    public RichInputText getTxtTenGroup() {
        return txtTenGroup;
    }

    public void setTxtDescription(RichInputText txtDescription) {
        this.txtDescription = txtDescription;
    }

    public RichInputText getTxtDescription() {
        return txtDescription;
    }
}
